package com.distance;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.bookmarks.BookmarksAdapter;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.overlays.LastKnownLocation;

public class EditBookmark extends MapActivity{

	private int bookmarkArrayPos;
	private EditText locationNameEditText;
	private EditText locationDescriptionEditText;
	private TextView longitudeTextView;
	private TextView latitudeTextView;
	private MapView mapView;
	private Button saveBookmarkButton;
	private Button cancelBookmarkButton;
	private Button deleteBookmarkButton;
	private Button editBookmarkPointButton;
	private Button saveBookmarkPointButton;
	private Button cancelBookmarkPointButton;
	private ZoomControls zoomControls;

	private Drawable whitePixel;
	private Drawable bookmarkPointer;

	private GeoPoint geoPoint;
	private GeoPoint tempGeoPoint;

	private BookMarkPoint bookmarkPointOverlay;
	private LastKnownLocation tempGeoPointOverlay;
	private LastKnownLocation bookmarkGeoPointOverlay;

	private boolean mapEditable = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editbookmark);
		//Set title of activity
		setTitle("Distance - Bookmark Editor");
		//instantiate overlay that covers the map view
		whitePixel = getResources().getDrawable(R.drawable.whitepixel);
		whitePixel.setBounds(0, 0, whitePixel.getIntrinsicWidth(), whitePixel.getIntrinsicHeight());
		bookmarkPointer = getResources().getDrawable(R.drawable.red_pointer);
		bookmarkPointer.setBounds(0, 0, bookmarkPointer.getIntrinsicWidth(), bookmarkPointer.getIntrinsicHeight());
		//instantiate rest of values
		bookmarkArrayPos = Bookmarks.detailedLocationPositionToEdit;
		locationNameEditText = (EditText)findViewById(R.id.EditBookmarkET01);
		locationDescriptionEditText =(EditText)findViewById(R.id.EditBookmarkET02);
		longitudeTextView = (TextView)findViewById(R.id.EditBookmarkTVLongitude);
		latitudeTextView = (TextView)findViewById(R.id.EditBookmarkTVLatitude);

		saveBookmarkButton = (Button)findViewById(R.id.EditBookmarkBSave);
		cancelBookmarkButton = (Button)findViewById(R.id.EditBookmarkBCancel);
		deleteBookmarkButton = (Button)findViewById(R.id.EditBookmarkBDelete);

		editBookmarkPointButton = (Button)findViewById(R.id.EditBookmarkBEdit);
		saveBookmarkPointButton = (Button)findViewById(R.id.EditBookmarkBMapOk);
		cancelBookmarkPointButton = (Button)findViewById(R.id.EditBookmarkBMapCancel);
		editBookmarkPointButton.setMaxHeight(5);
		cancelBookmarkPointButton.setVisibility(View.GONE);
		saveBookmarkPointButton.setVisibility(View.GONE);

		RelativeLayout zoomLayout = (RelativeLayout)findViewById(R.id.RelativeLayout01);
		//zoomLayout.

		bookmarkPointOverlay = new BookMarkPoint(whitePixel,null,getApplicationContext());
		geoPoint = Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getGeoPoint();

		tempGeoPoint = geoPoint;
		//init the button listeners
		initListeners();

		mapView = (MapView) findViewById(R.id.EditBookmarkMap);
		mapView.getController().setCenter(Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getGeoPoint());
		bookmarkGeoPointOverlay = new LastKnownLocation(bookmarkPointer, geoPoint, getApplicationContext());
		mapView.getOverlays().clear();
		mapView.invalidate();
		mapView.getOverlays().add(bookmarkGeoPointOverlay);

		zoomLayout.addView(mapView.getZoomControls());

		locationNameEditText.setText(Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getLocationName());
		locationDescriptionEditText.setText(Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getDescription());
		longitudeTextView.setText(((Double)(Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getGeoPoint().getLongitudeE6()/1E6)).toString());
		latitudeTextView.setText(((Double)(Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit).getGeoPoint().getLatitudeE6()/1E6)).toString());


		//latitudeTextView
		//((DetailedLocation)Bookmarks.locationList.get(Bookmarks.detailedLocationPositionToEdit-1).).
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void initListeners(){
		//create delete button listener
		this.deleteBookmarkButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					Bookmarks.locationList.remove(bookmarkArrayPos);
					Bookmarks.locationStrings.remove(bookmarkArrayPos);
					BookmarksAdapter.saveBookmarkList(Bookmarks.locationList);
					Intent intent = new Intent();
					intent.setClassName("com.distance", "com.distance.BookmarksList");
					startActivity(intent);
				}catch(Exception e){

				}
			}
		});
		//create cancel listener
		this.cancelBookmarkButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					Intent intent = new Intent();
					intent.setClassName("com.distance", "com.distance.BookmarksList");
					startActivity(intent);
				}catch(Exception e){

				}
			}
		});
		//create save listener
		this.saveBookmarkButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					Bookmarks.locationList.get(bookmarkArrayPos).setDescription(locationDescriptionEditText.getText().toString());
					//Bookmarks.locationList.get(bookmarkArrayPos).setGeoPoint(geoPoint);
					Bookmarks.locationList.get(bookmarkArrayPos).setLocationName(locationNameEditText.getText().toString());
					Bookmarks.locationList.get(bookmarkArrayPos).setGeoPoint(geoPoint);
					Bookmarks.locationStrings.set(bookmarkArrayPos, locationNameEditText.getText().toString());
					//Bookmarks.locationStrings.remove(bookmarkArrayPos);
					BookmarksAdapter.saveBookmarkList(Bookmarks.locationList);
					Intent intent = new Intent();
					intent.setClassName("com.distance", "com.distance.BookmarksList");
					startActivity(intent);
				}catch(Exception e){

				}
			}
		});


		this.editBookmarkPointButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					mapView.getOverlays().remove(bookmarkPointOverlay);
					mapView.invalidate();
					mapView.getOverlays().add(bookmarkPointOverlay);
					cancelBookmarkPointButton.setVisibility(View.VISIBLE);
					saveBookmarkPointButton.setVisibility(View.VISIBLE);
					editBookmarkPointButton.setVisibility(View.GONE);

					saveBookmarkButton.setVisibility(View.GONE);
					deleteBookmarkButton.setVisibility(View.GONE);
					cancelBookmarkButton.setVisibility(View.GONE);

					locationNameEditText.setVisibility(View.GONE);
					locationDescriptionEditText.setVisibility(View.GONE);
				}catch(Exception e){

				}
			}
		});


		this.cancelBookmarkPointButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					mapView.getOverlays().remove(bookmarkPointOverlay);
					mapView.getOverlays().remove(tempGeoPointOverlay);
					mapView.invalidate();
					tempGeoPointOverlay = null;
					longitudeTextView.setText(((Double)(geoPoint.getLongitudeE6()/1E6)).toString());
					latitudeTextView.setText(((Double)(geoPoint.getLatitudeE6()/1E6)).toString());
					cancelBookmarkPointButton.setVisibility(View.GONE);
					saveBookmarkPointButton.setVisibility(View.GONE);
					editBookmarkPointButton.setVisibility(View.VISIBLE);

					saveBookmarkButton.setVisibility(View.VISIBLE);
					deleteBookmarkButton.setVisibility(View.VISIBLE);
					cancelBookmarkButton.setVisibility(View.VISIBLE);

					locationNameEditText.setVisibility(View.VISIBLE);
					locationDescriptionEditText.setVisibility(View.VISIBLE);
				}catch(Exception e){

				}
			}
		});
		this.saveBookmarkPointButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					geoPoint = tempGeoPoint;
					mapView.getOverlays().remove(bookmarkPointOverlay);
					mapView.invalidate();
					if(tempGeoPointOverlay!=null){
						mapView.getOverlays().remove(bookmarkGeoPointOverlay);
						mapView.invalidate();
						bookmarkGeoPointOverlay = tempGeoPointOverlay;
						mapView.getOverlays().add(bookmarkGeoPointOverlay);
						tempGeoPointOverlay = null;
					}
					cancelBookmarkPointButton.setVisibility(View.GONE);
					saveBookmarkPointButton.setVisibility(View.GONE);
					editBookmarkPointButton.setVisibility(View.VISIBLE);

					saveBookmarkButton.setVisibility(View.VISIBLE);
					deleteBookmarkButton.setVisibility(View.VISIBLE);
					cancelBookmarkButton.setVisibility(View.VISIBLE);

					locationNameEditText.setVisibility(View.VISIBLE);
					locationDescriptionEditText.setVisibility(View.VISIBLE);
				}catch(Exception e){

				}
			}
		});
	}

	public class BookMarkPoint extends Overlay{

		BitmapDrawable bmp;
		int w, h;
		Point p;
		private Context context;

		public BookMarkPoint(Drawable d, Point p, Context _context) { 
			context = _context;
			bmp = (BitmapDrawable) d; 
			this.p = p; 
			w = bmp.getIntrinsicWidth(); 
			h = bmp.getIntrinsicHeight(); 
		} 

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			bmp.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
			bmp.setAlpha(0);
			bmp.draw(canvas);
		}

		@Override
		public boolean onTap(GeoPoint p, MapView mapView) {
			Toast.makeText(context, p.toString(), Toast.LENGTH_SHORT).show();
			mapView.getOverlays().remove(tempGeoPointOverlay);
			mapView.invalidate();
			tempGeoPoint = p;
			tempGeoPointOverlay = new LastKnownLocation(bookmarkPointer, p, getApplicationContext());
			mapView.getOverlays().add(tempGeoPointOverlay);
			longitudeTextView.setText(((Double)(tempGeoPoint.getLongitudeE6()/1E6)).toString());
			latitudeTextView.setText(((Double)(tempGeoPoint.getLatitudeE6()/1E6)).toString());
			//addWayPointLogic(p);
			return true;
		}

	}


}
